package org.circedroid.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.io.*;

import android.util.Log;
import android.content.Context;
import android.widget.Toast;


public class Data{

public HashMap<Integer,ELG> listELG = new HashMap<Integer,ELG>();
public HashMap<Integer,LGO> listLGO= new HashMap<Integer,LGO>();
public HashMap<Integer,REG> listREG = new HashMap<Integer,REG>();
public HashMap<Integer,REA> listREA = new HashMap<Integer,REA>();
public HashMap<Integer,TSG0> listTSG0 = new HashMap<Integer,TSG0>();
public HashMap<Integer,TSG> listTSG = new HashMap<Integer,TSG>();
public HashMap<Integer,PRC> listPRC = new HashMap<Integer,PRC>();
public HashMap<Integer,PROJ> listPROJ = new HashMap<Integer,PROJ>();
public HashMap<Integer,GR1D> listGR1D = new HashMap<Integer,GR1D>();
public HashMap<Integer,GR3D> listGR3D = new HashMap<Integer,GR3D>();
public HashMap<Integer,ZNE> listZNE = new HashMap<Integer,ZNE>();
public HashMap<Integer,TYPCOOR> listTYPCOOR = new HashMap<Integer,TYPCOOR>();
public HashMap<Integer,UNIT> listUNIT = new HashMap<Integer,UNIT>();
public Context context = null;
public String packagename;
public Data(Context context){
	this.context=context;
	int file = this.context.getResources().getIdentifier("data","raw","org.circedroid.activity");
	try{
		InputStream ips = context.getResources().openRawResource(file); 
		InputStreamReader ipsr=new InputStreamReader(ips);
		BufferedReader br=new BufferedReader(ipsr);
		String ligne;
		
		while ((ligne=br.readLine())!=null){
			if (ligne.length()>0 && (Character) ligne.charAt(0)=='*'){
				continue;
			}
			else {
				if (ligne.contains("ELG")){
					String[] mots=ligne.split("[ \t]+", 7);
					listELG.put(Integer.parseInt(mots[1]), new ELG(Double.parseDouble(mots[2]), Double.parseDouble(mots[3]), Double.parseDouble(mots[4]), Double.parseDouble(mots[5]), mots[6]));
				}
				else if (ligne.contains("LGO")) {
					String[] mots=ligne.split("[ \t]+", 5);
					listLGO.put(Integer.parseInt(mots[1]), new LGO(Integer.parseInt(mots[2]), Double.parseDouble(mots[3]), mots[4])); 
				}
				else if (ligne.contains("REG")) {
					String[] mots=ligne.split("[ \t]+", 6);
					listREG.put(Integer.parseInt(mots[1]), new REG(Integer.parseInt(mots[2]), Integer.parseInt(mots[3]), Short.parseShort(mots[4].substring(0,1)), mots[5]));
				}
				else if (ligne.contains("REA")) {
					String[] mots=ligne.split("[ \t]+", 3);
					listREA.put(Integer.parseInt(mots[1]), new REA(mots[2]));
				}
				else if (ligne.contains("TSG0")) {
					String[] mots=ligne.split("[ \t]+", 6);
					listTSG0.put(listTSG0.size()+1, new TSG0(Integer.parseInt(mots[1]), Integer.parseInt(mots[2]), Double.parseDouble(mots[3]), Double.parseDouble(mots[4]), Double.parseDouble(mots[5])));
				}
				else if (ligne.contains("TSG")) {
					String[] mots=ligne.split("[ \t]+", 12);
					listTSG.put(listTSG.size()+1, new TSG(Integer.parseInt(mots[2]), Integer.parseInt(mots[3]), Integer.parseInt(mots[4]), Double.parseDouble(mots[5]), Double.parseDouble(mots[6]), Double.parseDouble(mots[7]), Double.parseDouble(mots[8]), Double.parseDouble(mots[9]), Double.parseDouble(mots[10]), Double.parseDouble(mots[11])));
				}
				else if (ligne.contains("PRC")) {
					String[] mots=ligne.split("[ \t]+", 16);
					listPRC.put(Integer.parseInt(mots[1]), new PRC(Integer.parseInt(mots[2]), Integer.parseInt(mots[3]), Double.parseDouble(mots[4]), Double.parseDouble(mots[5]), Integer.parseInt(mots[6]), Integer.parseInt(mots[7]), Double.parseDouble(mots[8]), Double.parseDouble(mots[9]), Double.parseDouble(mots[10]), Double.parseDouble(mots[11]), Double.parseDouble(mots[12]), Integer.parseInt(mots[13]), Integer.parseInt(mots[14]), mots[15]));
				}
				else if (ligne.contains("PROJ")) {
					String[] mots=ligne.split("[ \t]+", 8);
					listPROJ.put(listPROJ.size()+1, new PROJ(Integer.parseInt(mots[1]), Integer.parseInt(mots[2]), Integer.parseInt(mots[3]), Double.parseDouble(mots[4]), Double.parseDouble(mots[5]), Double.parseDouble(mots[6]), Double.parseDouble(mots[7])));
				}
				else if (ligne.contains("GR1D")) {
					String[] mots=ligne.split("[ \t]+");
					listGR1D.put(listGR1D.size()+1,new GR1D(mots[1], context,Integer.parseInt(mots[2])));
					for (int i=3;i<mots.length;i++){
						listGR1D.get(listGR1D.size()).addREA(Integer.parseInt(mots[i]));
					}
				}
				else if (ligne.contains("GR3D")) {
					String[] mots=ligne.split("[ \t]+", 4);
					listGR3D.put(listGR3D.size()+1, new GR3D(mots[1], context, Integer.parseInt(mots[2]), Integer.parseInt(mots[3])));
				}
				else if (ligne.contains("ZNE")) {
					String[] mots=ligne.split("[ \t]+", 7);
					listZNE.put(Integer.parseInt(mots[1]), new ZNE(Integer.parseInt(mots[1]), Integer.parseInt(mots[2]), Double.parseDouble(mots[3]), Double.parseDouble(mots[4]), Double.parseDouble(mots[5]), Double.parseDouble(mots[6])));
				}
				else if (ligne.contains("TYPCOOR")) {
					String[] mots=ligne.split("[ \t]+");
					listTYPCOOR.put(Integer.parseInt(mots[1]), new TYPCOOR(Integer.parseInt(mots[1])));
					for (int i=2;i<mots.length;i++){
						if (Integer.parseInt(mots[i])==1){
							listTYPCOOR.get(Integer.parseInt(mots[1])).addCart();
						}
						else if (Integer.parseInt(mots[i])==2){
							listTYPCOOR.get(Integer.parseInt(mots[1])).addGeo();
						}
						else if (Integer.parseInt(mots[i])==3){
							listTYPCOOR.get(Integer.parseInt(mots[1])).addPlan();
						}
					}
				}
				else if (ligne.contains("UNIT")) {
					String[] mots=ligne.split("[ \t]+");
					listUNIT.put(Integer.parseInt(mots[1]), new UNIT(Integer.parseInt(mots[1])));
					if (mots.length==2){
						listUNIT.get(Integer.parseInt(mots[1])).addAllUnit();
					}
					else {
						for (int i=2;i<mots.length;i++){
							listUNIT.get(Integer.parseInt(mots[1])).addUnit(Integer.parseInt(mots[i]));
						}
					}
				}
			}
		}        //regdep
		br.close();
		//Ajout des TSG entre reg de mm ellip
		Set<Integer> regset=listREG.keySet();
		List<Integer> listreg = new ArrayList<Integer>();
		Iterator<Integer> iter=regset.iterator();
		for (int i=0;i<listREG.size();i++){
			listreg.add(iter.next());
		}
		
		for (int i=0;i<listREG.size();i++){
			for (int j=(i+1);j<listREG.size();j++){

				if (listREG.get(listreg.get(i)).getELG()==listREG.get(listreg.get(j)).getELG()){
					/*
					//Les autres transfo avant la transfo nulle
					int mysize=listTSG.size();
					for (int k=1;k<=mysize;k++){
						if (listTSG.get(k).reg1==listreg.get(i)){
							listTSG.put(listTSG.size()+1, new TSG(listTSG.get(k)));
							listTSG.get(listTSG.size()).reg1=listreg.get(j);
							continue;
						}
						if (listTSG.get(k).reg2==listreg.get(i)){
							listTSG.put(listTSG.size()+1, new TSG(listTSG.get(k)));
							listTSG.get(listTSG.size()).reg2=listreg.get(j);
							continue;
						}
						if (listTSG.get(k).reg1==listreg.get(j)){
							listTSG.put(listTSG.size()+1, new TSG(listTSG.get(k)));
							listTSG.get(listTSG.size()).reg1=listreg.get(i);
							continue;
						}
						if (listTSG.get(k).reg2==listreg.get(j)){
							listTSG.put(listTSG.size()+1, new TSG(listTSG.get(k)));
							listTSG.get(listTSG.size()).reg2=listreg.get(i);
							continue;
						}
					}
					*/
					listTSG.put(listTSG.size()+1, new TSG(listreg.get(i), listreg.get(j), 0, 0, 0, 0, 0, 0, 0, 0));
					
				}
			}
		}
		
	}		
	catch (Exception e){
		Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
	}
}
public int getRegNum(String reg){
	int num=0;
	Set<Integer> keyset=this.listREG.keySet();
	Iterator<Integer> iter = keyset.iterator();
	int current;
	while (iter.hasNext()){
		current=iter.next();
		if (this.listREG.get(current).getNom()==reg){
			num=current;
			break;
		}
	}
	return num;
}
public int getLgoNum(String reg){
	int num=0;
	Set<Integer> keyset=this.listLGO.keySet();
	Iterator<Integer> iter = keyset.iterator();
	int current;
	while (iter.hasNext()){
		current=iter.next();
		if (this.listLGO.get(current).getNom().equals(reg)){
			num=current;
			break;
		}
	}
	return num;
}
public int getProjNum(String reg){
	int num=0;
	Set<Integer> keyset=this.listPROJ.keySet();
	Iterator<Integer> iter = keyset.iterator();
	int current;
	while (iter.hasNext()){
		current=iter.next();
		if (this.listPRC.get(this.listPROJ.get(current).getPRC()).getNom()==reg){
			num=current;
			break;
		}
	}
	return num;
}
public int getReaNum(String reg){
	int num=0;
	Set<Integer> keyset=this.listREA.keySet();
	Iterator<Integer> iter = keyset.iterator();
	int current;
	while (iter.hasNext()){
		current=iter.next();
		if (this.listREA.get(current).getNom()==reg){
			num=current;
			break;
		}
	}
	return num;
}

}
